package net.knightwatch.kwtools;

import java.util.ArrayList;

import net.knightwatch.kwtools.customers.Customer;
import net.knightwatch.kwtools.customers.CustomerArrayAdapter;
import net.knightwatch.kwtools.database.KwToolsDatasource;
import android.app.ListActivity;
import android.content.Intent;
import android.database.SQLException;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

public class CustomerList extends ListActivity {
	private static final String TAG = "CustomerList";
	private CustomerArrayAdapter arrayAdapter;	
	private KwToolsDatasource datasource = new KwToolsDatasource(this);
	private ArrayList<Customer> customers;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Init the array list
        customers = new ArrayList<Customer>();
        // Assign layout and customer list to the array adapter
        arrayAdapter = new CustomerArrayAdapter(this, R.layout.customer_list_item, customers);
        // Set the source of the list
        setListAdapter(arrayAdapter);
        // Query the database for the list of people to populate the list with.
        // Register the context menu
        registerForContextMenu(getListView());
        
	}
	
	@Override
	protected void onResume() {
		super.onResume();
        updateListFromDb();		
	}
	
	private void updateListFromDb() {
		try{
			customers.clear();        	
			datasource.Open();
        	for(Customer c : datasource.getCustomerList()){
        		customers.add(c);
        	}
        	arrayAdapter.notifyDataSetChanged();
        	Log.d(TAG, "Updated the list of Customers");
        } catch (SQLException e){
        	e.printStackTrace();
        	Log.e(TAG, "Unable to read customers from datasource");
        } finally {
        	datasource.Close();
        }
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.customer_list_activity_menu, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
	    case R.id.addCustomer:
	        	newCustomerActivity();
	        	return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}

	private void newCustomerActivity() {
		Log.v(TAG, "Starting the add new customer activity");
		Intent intent = new Intent(this, CustomerActivity.class);
		intent.putExtra("readOnly", false);
		startActivity(intent);
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		// Create long press context menu for customer list
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.customer_list_context_menu, menu);
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		// Handle items pressed from long press context menu
		// Build the intent
		Intent intent = null;
		Customer customer = new Customer();
		AdapterView.AdapterContextMenuInfo info = null;
		try {
			info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
			customer = customers.get(info.position);
		} catch (Exception e){
			Log.e(TAG, "Error " + e.toString());
		}
		switch (item.getItemId()){
		case R.id.addBuilding:
				intent = new Intent(this, BuildingActivity.class);
				intent.putExtra("customerId", customer.getCustomerId());
				startActivity(intent);
				return true;
		case R.id.addPotential:
				intent = new Intent(this, PotentialActivity.class);
				intent.putExtra("customerId", customer.getCustomerId());
				startActivity(intent);
				return true;
			default:
				return super.onContextItemSelected(item);
		}
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		Customer customer = new Customer();
		customer = (Customer) getListAdapter().getItem(position);
		// Open read only view of customer object
		// Create intent
		Log.d(TAG, "Starting Intent customerId = " + customer.getCustomerId());
		Intent intent = new Intent(this, CustomerActivity.class);
		intent.putExtra("customerId", customer.getCustomerId());
		intent.putExtra("readOnly", true);
		// Start intent
		startActivity(intent);
	}
}